package com.bytedance.ttnet.hostmonitor;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.bytedance.ttnet.hostmonitor.Logger.LogLevel;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public final class e {
    public final Context a;
    public Map b;
    public String c;
    public int d;
    public int e;
    public int f;
    private SharedPreferences g;

    public e(Context arg2) {
        this.d = -1;
        this.e = -1;
        this.f = -1;
        this.a = arg2.getApplicationContext();
    }

    public final SharedPreferences a() {
        if (this.g == null) {
            this.g = this.a.getSharedPreferences("host_monitor_config", 0);
        }

        return this.g;
    }

    public final boolean a(b arg3) {
        if (arg3 == null) {
            boolean v0 = false;
            return v0;
        }

        try {
            Map v0_2 = this.b();
            if (v0_2 == null) {
                return false;
            }

            Object v0_3 = v0_2.get(arg3);
            if (v0_3 != null) {
                if (!((h) v0_3).a) {
                    return false;
                }

                return true;
            }

            return false;
        } catch (Throwable v0_1) {
            return false;
        }
    }

    public final Map b() {
        try {
            if (this.b != null) {
                return this.b;
            }

            String v1 = this.a().getString("hosts", "");
            if (v1.isEmpty()) {
                this.b = new ConcurrentHashMap();
                return this.b;
            }

            Type v0_1 = new TypeToken<ConcurrentHashMap<b, h>>(){}.getType();
            try {
                this.b = (Map) new Gson().fromJson(v1, v0_1);
            } catch (Exception v0_2) {
                try {
                    String v2 = "HostMonitor";
                    v1 = "Error while deserializing hosts map: " + v1 + ". Ignoring values.";
                    if (com.bytedance.ttnet.hostmonitor.Logger.b.a().a.compareTo(LogLevel.ERROR) <= 0) {
                        com.bytedance.ttnet.hostmonitor.Logger.b.a();
                        Log.e(v2, v1, ((Throwable) v0_2));
                    }

                    this.b = new ConcurrentHashMap();
                } catch (Throwable v0) {
                    label_42:
                    v0.printStackTrace();
                }
            }
        } catch (Throwable v0) {
            v0.printStackTrace();
        }

        label_11:
        return this.b;
    }

    public final String c() {
        if (this.c == null) {
            this.c = this.a().getString("broadcastAction", "com.bytedance.ttnet.hostmonitor.status");
        }
        return this.c;
    }

    public final int d() {
        if (this.e <= 0) {
            this.e = this.a().getInt("checkInterval", 0);
        }

        return this.e;
    }

    final void e() {
        try {
            Logger.a("HostMonitor", "saving hosts status map");
            GsonBuilder v0_1 = new GsonBuilder();
            v0_1.serializeNulls();
            this.a().edit().putString("hosts", v0_1.create().toJson(this.b)).apply();
        } catch (Throwable v0) {
            v0.printStackTrace();
        }
    }
}

